import {AsyncStorage} from 'react-native';
import SQLite from './SQLite';
let angryMood = require('./image/angry.png');
let happyMood = require('./image/happy.png');
let peaceMood = require('./image/peace.png');
let sadMood = require('./image/sad.png');
let miseryMood = require('./image/misery.png');
var sqlite = new SQLite();
var db;
export default class DataHandler {
  static diaryList = [];
  static listIndex = 0;
  static getAllDiary() {
    return new Promise(function (resolve, reject) {
      db = sqlite.open();
      db.executeSql(
        'select * from Diary',
        [],
        (tx, results) => {
          var len = results.rows.length;
          if (len > 0) {
            DataHandler.diaryList = results;
            console.log('result', results);
            resolve(DataHandler.diaryList);
          } else {
            resolve(DataHandler.diaryList);
            console.log('列表没有数据');
            return;
          }
        },
        (err) => {
          console.log(err);
        },
      );
    }).catch((err) => {
      console.log(err);
    });
  }
  //保存
  static saveDiary(newDiaryMood, newDiaryBody, newDiaryTitle) {
    return new Promise(function (resolve, reject) {
      //   console.log('test:', newDiaryMood + newDiaryBody + newDiaryTitle);
      let aDiary = Object();
      aDiary.title = newDiaryTitle;
      aDiary.body = newDiaryBody;
      let newMoodIcon;
      switch (newDiaryMood) {
        case 1:
          newMoodIcon = peaceMood;
          break;
        case 2:
          newMoodIcon = happyMood;
          break;
        case 3:
          newMoodIcon = sadMood;
          break;
        case 4:
          newMoodIcon = angryMood;
          break;
        case 5:
          newMoodIcon = miseryMood;
          break;
        default:
          newMoodIcon = peaceMood;
          break;
      }
      sqlite.insertData(aDiary);
    });
  }
}
